﻿using ATM.DAO;
using ATM.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ATMSystem
{
    public partial class frmViewHistory : Form
    {
        ViewHistoryDAO vhDAO = new ViewHistoryDAO();
        private Card passCard;
        public frmViewHistory()
        {
            InitializeComponent();
        }
        public frmViewHistory(Card card)
        {
            InitializeComponent();
            passCard = card;
        }
        frmInsertCard frmIC= new frmInsertCard();
        public int j = 0;
        public int i = 1;
        public int k = 0;
        public void addToListView(List<ViewHistory> lst_view)
        {
            lview.Items.Clear();
            foreach (ViewHistory vh in lst_view)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = vh.Address;
                lvi.SubItems.Add(vh.Type);
                lvi.SubItems.Add(vh.date.ToShortDateString());
                lvi.SubItems.Add(vh.Amount.ToString());
                lvi.SubItems.Add(vh.To);

                lview.Items.Add(lvi);
            }
        }
        
        public void visibleFrm(bool a)
        {
            lb1week.Visible = lb1month.Visible = lb4month.Visible = lb6month.Visible = lb1year.Visible = lb2year.Visible = label1.Visible = a;
            lbQuit.Visible = lview.Visible = !a;
        }

        public List<ViewHistory> getLink(int k)
        {
            string a = "123456789ABCDEFG";
            List<ViewHistory> lst = new List<ViewHistory>();
            switch (k)
            {
                case 1: lst = vhDAO.GetData(passCard.CardNo, "wk", 1); break;
                case 2: lst = vhDAO.GetData(passCard.CardNo, "mm", 1); break;
                case 3: lst = vhDAO.GetData(passCard.CardNo, "mm", 4); break;
                case 4: lst = vhDAO.GetData(passCard.CardNo, "mm", 6); break;
                case 5: lst = vhDAO.GetData(passCard.CardNo, "yyyy", 1); break;
                case 6: lst = vhDAO.GetData(passCard.CardNo, "yyyy", 2); break;
            }
            return lst;
        }
        private void BtnClick(object sender, EventArgs e)
        {
            Button btn = sender as Button;

            switch (btn.Name)
            {
                case "btn1":
                    if (lb1week.Visible)
                    {
                        k = 1;
                        nextAndPre(getLink(k), 1);
                        visibleFrm(false);
                    }

                    break;
                case "btn2":
                    if (lb1month.Visible)
                    {
                        k = 2;
                        nextAndPre(getLink(k), 1);
                        visibleFrm(false);
                    }

                    break;
                case "btn3":
                    if (lb4month.Visible)
                    {
                        k = 3;
                        nextAndPre(getLink(k), 1);
                        visibleFrm(false);
                    }
                    else
                        if (lbQuit1.Text == "QUIT")
                        {
                            visibleFrm(true);
                            lbQuit1.Visible = false;
                            i = 1;
                            j = 0;
                        }

                        else 
                            if (lbQuit1.Text == "PRE")
                            {
                                i--;
                                j -= lview.Items.Count + 5;
                                nextAndPre(getLink(k), i);
                            }

                    break;
                case "btn4":
                    if (lb6month.Visible)
                    {
                        k = 4;
                        nextAndPre(getLink(k), 1);
                        visibleFrm(false);
                    }
                    break;
                case "btn5":
                    if (lb1year.Visible)
                    {
                        k = 5;
                        nextAndPre(getLink(k), 1);
                        visibleFrm(false);
                    }
                    break;
                case "btn6":
                    if (lb2year.Visible)
                    {
                        k = 6;
                        nextAndPre(getLink(k), 1);
                        visibleFrm(false);
                    }
                    else
                        if (lbQuit.Text == "NEXT")
                        {
                            lb2year.Visible = false;
                            i++;
                            nextAndPre(getLink(k), i);
                        }
                        else
                            if (lbQuit.Text == "QUIT")
                            {
                                visibleFrm(true);
                                lbQuit1.Visible = false;
                                lbQuit1.Text = "";
                                i = 1; j = 0;
                            }
                    break;
            }
        }

        public void nextAndPre(List<ViewHistory> lvh, int i)
        {

            int k = lvh.Count / 5;
            if (lvh.Count % 5 != 0) k += 1;
            List<ViewHistory> lsv_new = new List<ViewHistory>();
            if (i <= k)
            {
                while (j < 5 * i && j < lvh.Count)
                {

                    lsv_new.Add(lvh[j]);
                    j++;
                }
                addToListView(lsv_new);

                lbQuit.Text = i.ToString();
            }
            if (i == 1 && i == k)
            {
                lbQuit1.Text = "QUIT";
                lbQuit.Text = "";
                lbQuit1.Visible = true;
            }
            else
                if (i == k)
                {
                    lbQuit1.Text = "PRE";
                    lbQuit1.Visible = true;
                    lbQuit.Text = "QUIT";
                }
                else
                    if (i == 1 && i != k)
                    {
                        lbQuit1.Visible = true;
                        lbQuit1.Text = "QUIT";
                        lbQuit.Text = "NEXT";
                    }
                    else
                    {
                        lbQuit1.Text = "PRE";
                        lbQuit1.Visible = true;
                        lbQuit.Text = "NEXT";
                        lbQuit.Visible = true;
                    }
        }

        private void frmViewHistory_Load(object sender, EventArgs e)
        {
            visibleFrm(true);
            
        }
    }
}
